package com.css.dzj.dzjap.modules.lawenforcesuper.controller;


import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.info.entity.ApExpertEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApExpertRegulatorInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApExtractExpertEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApExtractLawerEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.ApExpertRegulatorService;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.ApExtractExpertService;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.ExpregulatConnectExpertService;
import com.css.dzj.dzjap.modules.lawenforcesuper.view.ApExtractExpertView;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 审查专家监管信息constroller
 * @author liyunyue
 */
@RequestMapping("/expertRegulat")
@RestController
public class ApExpertRegulatorInfoController {


    @Autowired
    private ApExpertRegulatorService apExpertRegulatorService;

    @Autowired
    private ExpregulatConnectExpertService connectExpertService;

    @Autowired
    private ApExtractExpertService apExtractExpertService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    /**
     * 抽取专家
     * @param
     * @return
     */
    @PostMapping("/extract")
    public R extract(@RequestParam String title, @RequestBody List<ApExpertRegulatorInfoEntity> regulatorInfoEntityList, HttpServletRequest request) {
        String s = "";
        try {
            if(StringUtils.isEmpty(title) || StringUtils.isEmpty(regulatorInfoEntityList) || regulatorInfoEntityList.size() == 0) {
                return R.error("标题或抽取条件为空！");
            }
            ApExtractExpertEntity apExtractExpertEntity = new ApExtractExpertEntity();
            apExtractExpertEntity.setTitle(title);
            R result = apExpertRegulatorService.extract(apExtractExpertEntity, regulatorInfoEntityList, request);

            if("500".equals(result.get("code").toString())) {
                return R.error((result.get("msg")).toString());
            }
            //TODO
            s = JSON.toJSONString(result);
            sysAuditService.saveSuccessAudit("抽取成功", Constant.EXPERTREGULAT, Constant.PT, Constant.EXTRACTEXPERT, s, request);
            return R.ok().put("taskId", result.get("taskId"));
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("抽取失败", Constant.EXPERTREGULAT, Constant.YZ, Constant.EXTRACTEXPERT, s, e, request);
            return R.error(e.getMessage());
        }

    }

    /**
     * 查询所有抽取专家任务
     * @param title
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryAll")
    public R queryExpertRegulator(@RequestParam(required = false) String title,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                  HttpServletRequest request) {
        String s = "";
       try{
           PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
           QueryResults<ApExtractExpertEntity> expertEntityQueryResults = apExpertRegulatorService.queryAll(pageRequest, title, request);
           s = JSON.toJSONString(expertEntityQueryResults);
           sysAuditService.saveSuccessAudit("查询成功", Constant.EXPERTREGULAT, Constant.PT, Constant.SELECT, s, request);
           return R.ok().put("data", expertEntityQueryResults);
       } catch(Exception e) {
           e.printStackTrace();
           s = JSON.toJSONString(null);
           sysAuditService.saveFailAudit("查询失败", Constant.EXPERTREGULAT, Constant.PT, Constant.SELECT, s, e, request);
           return R.error(e.getMessage());
       }

    }

    /**
     * 通过监管任务Id分页查询专家list
     * @param taskId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/queryByTaskId")
    public R queryExpertByRegulatId(@RequestParam(required = false) String taskId,
                                    @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
//            ApExpertRegulatorInfoEntity regulatorInfoEntity = regulatorService.queryById(regulatId);
//            QueryResults<ApExpertEntity> extractExpertList = connectExpertService.queryExpertByRegulateId(pageRequest, regulatId);
            // 通过id查询抽取专家任务
            ApExtractExpertEntity apExtractExpertEntity = apExtractExpertService.queryById(taskId);
            // 通过任务id查询专家抽取信息list
            List<ApExpertRegulatorInfoEntity> apExpertRegulatorInfoEntities = apExpertRegulatorService.queryRegulatorByTaskId(taskId);
            QueryResults<ApExpertEntity> apExpertEntityQueryResults = connectExpertService.queryExpertByTaskId(pageRequest, taskId);
            //TODO
            s = JSON.toJSONString(apExpertEntityQueryResults);
            sysAuditService.saveSuccessAudit("查询成功", Constant.EXPERTREGULAT, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("apExtractExpertEntity", apExtractExpertEntity).put("apExpertRegulatorInfoEntities", apExpertRegulatorInfoEntities)
                    .put("apExpertEntityQueryResults", apExpertEntityQueryResults);
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.EXPERTREGULAT, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }

    }


}
